package com.ctu.gzero.conversion
{
	import com.ctu.gzero.elements.DOMElement;
	import com.ctu.gzero.elements.ObjectRun;
	import com.ctu.gzero.elements.ParaElement;
	import com.ctu.gzero.elements.TxtRun;
	
	import mx.collections.ArrayCollection;
	
	public class DomExporter
	{
		public function DomExporter()
		{
		}
		
		public static function exportXML(dom:DOMElement):XML
		{
			var paraListLength:int = 0;
			var runListLength:int = 0;
			var xml:XML =  <DomElement/>;
			xml.@label="DomElement";
			xml.@textLength = dom.textLength;
			xml.@type = "DomElement"; 
			
			paraListLength = dom.paraList.length;
			
			for(var i:int=0; i<paraListLength; i++)
			{
				var para:ParaElement = dom.getPara(i);
				var paraXML:XML = <ParaElement />;
				paraXML.@label = "ParaElement";
				paraXML.@id = para.id;
				paraXML.@type = "ParaElement";
				paraXML.@textLength = para.textLength;
				paraXML.@absoluteStart = para.absoluteStart;
				
				runListLength = para.runList.length;
				
				for(var j:int=0; j<runListLength; j++)
				{
					var textRun:TxtRun;
					var objectRun:ObjectRun;
					if(para.getRun(j).type == "TxtRun")
					{
						var textRunXML:XML = <TextRun ></TextRun>;
						textRun = para.getRun(j) as TxtRun;
						textRunXML.@label = "TxtRun";
						textRunXML.@id = textRun.id;
						textRunXML.@type = textRun.type;
						textRunXML.@textLength = textRun.textLength;
						textRunXML.@absoluteStart = textRun.absoluteStart;
						textRunXML.@relativeStart = textRun.relativeStart;
						textRunXML.@fontSize = textRun.fontSize;
						textRunXML.@lineHeight = textRun.lineHeight;
						textRunXML.@text = textRun.text;
						paraXML.appendChild(textRunXML);	
					} 
					else if (para.getRun(j).type == "ObjectRun")
					{
						var objectRunXML:XML = <ObjectRun ></ObjectRun>;
						objectRun = para.getRun(j) as ObjectRun;
						objectRunXML.@label = "ObjectRun";
						objectRunXML.@id = objectRun.id;
						objectRunXML.@type = objectRun.type;
						objectRunXML.@textLength = objectRun.textLength;
						objectRunXML.@absoluteStart = objectRun.absoluteStart;
						objectRunXML.@width = objectRun.width;
						objectRunXML.@height = objectRun.height;
						objectRunXML.@backgroundColor = objectRun.backgroundColor;
						objectRunXML.@alpha = objectRun.alpha;
						paraXML.appendChild(objectRunXML);
					}
					
					
				}
				
				xml.appendChild(paraXML);
			}
			
			return xml;
		}
		
		public static function exportDom(xml:XML):ArrayCollection
		{
			var _col:ArrayCollection = new ArrayCollection();
			var _xmlList:XMLList = xml.attributes();
			return _col;
		}

	}
}